package com.unicom.office.impl;

import com.sun.org.apache.bcel.internal.generic.RETURN;
import com.unicom.office.config.OfficeConfig;
import com.unicom.office.node.*;
import com.unicom.office.parser.DocumentParser;
import com.unicom.office.parser.OfficeSyntaxParser;
import com.unicom.office.runtime.OfficeRuntime;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

import java.io.FileInputStream;
import java.util.List;

public class DocxParser extends DocumentParser {
    public DocxParser(){
        super(null, null);
    }

    public DocxParser(OfficeConfig config, OfficeRuntime runtime) {
        super(config, runtime);
    }

    protected DocumentNode parseDocument(Object source) {
        XWPFDocument document = (XWPFDocument)source;
        DocumentNode documentNode = new DocumentNode(document);

        // poi好像没有区分section，所以咯默认搞一个节点的啦
        SectionNode sectionNode = new SectionNode(source);
        documentNode.addChild(sectionNode);

        List<IBodyElement> bodyElements = document.getBodyElements();
        for (IBodyElement bodyElement : bodyElements) {
            parse(bodyElement, sectionNode);
        }

        return documentNode;
    }

    protected SectionNode parseSection(Object source, OfficeNode parentNode) {
        return null;
    }

    protected DocTableNode parseDocTable(Object source, OfficeNode parentNode) {
        return null;
    }

    public Object getSource(String sourcePath) {
        try {
            FileInputStream fileInputStream = new FileInputStream(sourcePath);
            String extString = sourcePath.substring(sourcePath.lastIndexOf("."));
            if (".docx".equals(extString)) {
                FileInputStream fis = new FileInputStream(sourcePath);
                XWPFDocument xdoc = new XWPFDocument(fis);
                return xdoc;
            } else {
                throw new RuntimeException("文件格式错误");
            }
        }catch(Exception ex){
            throw new RuntimeException(ex.getMessage());
        }
    }

    protected OfficeNodeType parseType(Object source) {
        if(source instanceof XWPFDocument){
            return OfficeNodeType.Document;
        }else if(source instanceof XWPFParagraph){
            return OfficeNodeType.Paragraph;
        }else if(source instanceof XWPFRun){
            return OfficeNodeType.TextRange;
        }else {
            throw new RuntimeException("暂时未实现");
        }
    }

    protected TextboxNode parseTextbox(Object source, OfficeNode parentNode) {
        return null;
    }

    protected RowNode parseRow(Object source, OfficeNode parentNode) {
        return null;
    }

    protected CellNode parseCell(Object source, OfficeNode parentNode) {
        return null;
    }

    protected ParagraphNode parseParagraph(Object source, OfficeNode parentNode) {
        XWPFParagraph paragraph = (XWPFParagraph) source;
        ParagraphNode paragraphNode = new ParagraphNode(paragraph);
        parentNode.addChild(paragraphNode);

        List<XWPFRun> runs = paragraph.getRuns();
        for (XWPFRun run : runs) {
            parse(run, paragraphNode);
        }

        OfficeSyntaxParser syntaxParser = new OfficeSyntaxParser(paragraphNode, config);
        syntaxParser.parse();

        return paragraphNode;
    }

    protected TextRangeNode parseTextRange(Object source, OfficeNode parentNode) {
        XWPFRun run = (XWPFRun)source;
        TextRangeNode textRangeNode = new TextRangeNode(run);
        parentNode.addChild(textRangeNode);

        String text = run.getText(0);
        textRangeNode.setText(text, false);

        return textRangeNode;
    }

    protected PictureNode parsePicture(Object source, OfficeNode parentNode) {
        return null;
    }
}
